package com.easy.dfc.web;

import com.alibaba.fastjson.JSONObject;
import com.easy.core.web.BaseController;
import com.easy.dfc.Utils.*;
import com.easy.dfc.entity.CartList;
import com.easy.dfc.entity.ProductsetWithBLOBs;
import com.easy.dfc.service.*;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by VULCANTSeries on 2017/5/26.
 */
@Controller
@RequestMapping("${apiPath}/Cart")
public class CartController extends BaseController {

    @Resource
    private CartService cartService;

    @Resource
    private TiantiantejiaService tiantiantejiaService;

    @Resource
    private TuangouService tuangouService;

    @Resource
    private TiaoweihuosetService tiaoweihuosetService;

    @Resource
    private UsersetService usersetService;

    /**
     * * 根据UserId获取该用户的购物车列表
     *
     * @param request
     * @return0
     */
    @RequestMapping(value = "/getCartList", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> getCartList(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpSession session = request.getSession();
        JSONObject jsonObject = new JSONObject();
        try {
            String userId = request.getParameter("UserId");
            String token = request.getParameter("Token");
            if (!usersetService.VerifyToken(userId, token)) {
                jsonObject.put("Err", com.easy.dfc.Utils.Error.ValidFailed);
                return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
            }
            List<CartList> cartLists = cartService.selectByUserId(Util.ToUuidString(userId));
            //遍历每条数据，判断是否在活动中
            for (CartList c : cartLists) {
                ProductsetWithBLOBs p = new ProductsetWithBLOBs();
                p.setPromotiontype(c.getPromotiontype());
                p.setProductid(c.getProductid());
                if (Util.PromotionStuts(p, tiantiantejiaService, tiaoweihuosetService, tuangouService)){
                    c.setPromotionStuts(1);
                }
            }
            String str = JSONObject.toJSONString(cartLists);
            jsonObject = JSONObject.parseObject(JsonUtil.toErrJson(com.easy.dfc.Utils.Error.OK, str));
        } catch (Exception e) {
            jsonObject.put("Err", com.easy.dfc.Utils.Error.OtherError);
            return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
        }
        return new ResponseEntity<JSONObject>(jsonObject, responseHeaders, HttpStatus.OK);
    }
}
